package com.room.search.web.rest;

import com.room.search.constant.UserConstants;
import com.room.search.core.page.TableDataInfo;
import com.room.search.domain.RoomDictData;
import com.room.search.domain.RoomDictType;
import com.room.search.repository.RoomDictDataRepository;
import com.room.search.repository.RoomDictTypeRepository;
import com.room.search.response.RoomResult;
import com.room.search.service.RoomDictDataService;
import com.room.search.service.RoomDictTypeService;
import com.room.search.service.dto.RoomDictDataDTO;
import com.room.search.service.dto.RoomDictTypeDTO;
import com.room.search.util.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * By   : ghd.bright
 * Time : 2022/2/15
 * Description: roomdictController
 */

@RestController
@RequestMapping("/api/dict")
public class RoomDictResource {
    private final RoomDictDataService roomDictDataService;
    private final RoomDictTypeService roomDictTypeService;

    public RoomDictResource(RoomDictDataService roomDictDataService, RoomDictTypeService roomDictTypeService) {
        this.roomDictDataService = roomDictDataService;
        this.roomDictTypeService = roomDictTypeService;
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/data/type/{dictType}")
    public RoomResult dictType(@PathVariable String dictType) {
        return roomDictDataService.getDataFromType(dictType);
    }

    /**
     * 所有字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/type/list")
    public TableDataInfo getAllDictType(RoomDictTypeDTO dto) {
        return roomDictTypeService.getAllDictType(dto);
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/type/{dictId}")
    public RoomResult getInfoByDictCode(@PathVariable Long dictId) {
        return RoomResult.success(roomDictTypeService.getInfoByDictCode(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping("/type/add")
    public RoomResult add(@Validated @RequestBody RoomDictTypeDTO dto) {
        return roomDictTypeService.add(dto);
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping("/type/edit")
    public RoomResult edit(@Validated @RequestBody RoomDictTypeDTO dto) {
        return roomDictTypeService.edit(dto);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/type/{dictIds}")
    public RoomResult removeType(@PathVariable Long[] dictIds) {

        return roomDictTypeService.deleteDictTypeByIds(dictIds);
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/type/refreshCache")
    public RoomResult refreshCache() {

        return roomDictTypeService.resetDictCache();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/type/optionselect")
    public RoomResult optionSelect() {
        return roomDictTypeService.selectDictTypeAll();
    }




    /**
     * 获取所有字典数据
     */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/data/list")
    public TableDataInfo list(RoomDictDataDTO dto) {
        return roomDictDataService.list(dto);
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/data/{dictCode}")
    public RoomResult getInfo(@PathVariable Long dictCode) {
        return RoomResult.success(roomDictDataService.selectDictDataById(dictCode));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping("/data/add")
    public RoomResult add(@Validated @RequestBody RoomDictDataDTO dto) {
        return roomDictDataService.insertDictData(dto);
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping("/data/edit")
    public RoomResult edit(@Validated @RequestBody RoomDictDataDTO dto)
    {
        return roomDictDataService.updateDictData(dto);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/data/{dictCodes}")
    public RoomResult removeData(@PathVariable Long[] dictCodes)
    {

        return roomDictDataService.deleteDictDataByIds(dictCodes);
    }

}
